<style>
  .header-font {
    font-size: 15px;
    font-weight: bold;
  }
  .line {
    margin: 10px 0;
    width: 100%;
    border: 0.5px solid rgb(218, 218, 218);
  }
  .table {
    margin-top: 10px;
  }
  .account-form {
    margin-bottom: -10px;
  }
  .paging {
    float:right;
    margin-top:10px;
  }
  .img{
   width: 150px;
   height: 100px;
   float: left;
   margin: 5px;
  }
</style>
<template>
  <div>
    <div class='header-font'>
      <Icon type="md-apps" />
      <span>账户管理</span>
    </div>
    <div class="line"></div>
    <account-form
      class="account-form"
      @on-success-valid="handleSubmit">
    </account-form>
    <Button
      icon="md-add"
      type="primary"
      @click="handleClick">
      添加账户
    </Button>
    <account-drawer
      :password="password"
      @account-valid="handleEdit">
    </account-drawer>
    <delete-modal
      @delete-valid="handleConfirmDelete">
    </delete-modal>
    <Table
      class="table"
      :columns="columns"
      :data="historyData"
      :loading="loading"
      border>
    </Table>
    <table-page
      class="paging"
      :dataCount="dataCount"
      @change-page="changePage"
      @change-page-size="changePageSize"
      :pageSize="pageSize">
    </table-page>
  </div>
</template>
<script>
import {
  mapActions,
  mapState
} from 'vuex'
import AccountForm from '_c/account-form'
import AccountDrawer from '_c/account-drawer'
import DeleteModal from '_c/delete-modal'
import TablePage from '_c/table-page'
export default {
  components: {
    AccountForm,
    AccountDrawer,
    DeleteModal,
    TablePage
  },
  data () {
    return {
      page: 1,
      title: '系统管理账户管理',
      index: '',
      confirmData: {},
      playload: null,
      rows: 20,
      loading: true,
      password: '',
      columns: [
        {
          title: '序号',
          type: 'index2',
          width: 80,
          align: 'center',
          render: (h, params) => {
            return h('span', params.index + (this.page - 1) * this.pageSize + 1)
          }
        },
        {
          title: '用户姓名',
          key: 'uname',
          minWidth: 150,
          align: 'center'
        },
        {
          title: '登录账号',
          key: 'tel',
          minWidth: 120,
          align: 'center'
        },
        {
          title: '角色',
          key: 'gname',
          minWidth: 130,
          align: 'center'
        },
        {
          title: '创建时间',
          key: 'ctime',
          minWidth: 150,
          align: 'center'
        },
        {
          title: '状态',
          key: 'state',
          align: 'center',
          minWidth: 100,
          render: (h, params) => {
            return h(
              'span',
              {},
              params.row.state === 0 ? '正常' : params.row.state === 1 ? '冻结' : '-'
            )
          }
        },
        {
          title: '邀请码',
          key: 'id',
          minWidth: 100,
          align: 'center',
          render: (h, params) => {
            if (params.row.id.toString().length < 2) {
              return h('span', '00' + params.row.id.toString())
            } else if (params.row.id.toString().length < 3) {
              return h('span', '0' + params.row.id.toString())
            } else {
              return h('span', params.row.id)
            }
          }
        },
        {
          title: '操作',
          key: 'cz',
          minWidth: 196,
          align: 'center',
          render: (h, params) => {
            return h('div', [
              h('Button', {
                props: {
                  type: 'primary',
                  size: 'small',
                  ghost: 'ghost'
                },
                style: {
                  marginRight: '5px'
                },
                on: {
                  click: () => {
                    this.handleClick(params.row.id, params.row)
                  }
                }
              }, '编辑')
            ])
          }
        }
      ]
    }
  },
  computed: {
    ...mapState({
      accountList: state => state.account.accountList,
      dataCount: state => state.account.accountDataCount,
      historyData: state => state.account.accountHistoryData,
      pageSize: state => state.account.accountPageSize
    })
  },
  methods: {
    ...mapActions([
      'getAccountList',
      'getRoleList',
      'getAccountCurrentRow',
      'editSysAccountData',
      'updateCustomerDrawerState',
      'updateDeleteModalState',
      'changeAccountPage',
      'changeAccountPageSize'
    ]),
    handleSubmit (playload) {
      this.loading = true
      this.playload = playload
      this.getAccountList(playload).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    },
    handleClick (id, row) {
      let newRow = {}
      this.id = id
      if (row) {
        this.password = row.password
        newRow = Object.assign({}, row)
      }
      this.getAccountCurrentRow(newRow)
      this.updateCustomerDrawerState(true)
    },
    handleDelete (index) {
      this.index = index
      this.updateDeleteModalState(true)
    },
    handleConfirmDelete () {
      this.accountList.splice(this.index + (this.page - 1) * this.pageSize, 1)
      this.updateDeleteModalState(false)
      this.changePage(this.page)
    },
    async handleEdit (playload) {
      this.loading = true
      const resp = await this.editSysAccountData(playload)
      if (resp.data.state === '000') {
        this.$Message.success('编辑成功')
        this.getAccountList({
          page: this.page,
          rows: this.rows,
          ...this.playload
        }).then((res) => {
          if (res.data.state === '000') {
            this.loading = false
          }
        })
      } else {
        this.$Message.error(resp.data.msg)
      }
      this.updateCustomerDrawerState(false)
    },
    changePage (index) {
      this.page = index
      this.loading = true
      this.changeAccountPage(index)
      this.getAccountList({
        page: index,
        rows: this.rows,
        ...this.playload
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    },
    changePageSize (value) {
      this.loading = true
      this.rows = value
      this.changeAccountPageSize(value)
      this.changePage(this.page)
      this.getAccountList({
        rows: value,
        ...this.playload
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    }
  },
  created () {
    this.getAccountList({}).then((res) => {
      if (res.data.state === '000') {
        this.loading = false
      }
    })
    this.getRoleList({})
  }
}
</script>
